<?php
require_once 'include/common.inc.php';
$max = 10000;
$count = 0;
$module_array = array(
    'article' => array(
        'detail' => array(
            'table' => 'article_list',
            'select' => array('id'),
            'term' => array('status'=>'NEED_GENERATE'),
            'order_by' => array('id'),
            'order_type' => 'DESC',
            'parameter' => array('article_id'=>'id'),
            'parameter_name' => 'article_id',
            'update' => array('status'=>'GENERATED','url'=>'url'),
            ),
    ),
);


$generate_array = array();

if(!empty($module) && !empty($page) && !empty($module_array[$module][$page])){
    $generate_array = array($module => array($page => $module_array[$module][$page]));
}elseif(!empty($module) && !empty($module_array[$module])){
    $generate_array = array($module => $module_array[$module]);
}else{
    $generate_array = $module_array;
}

foreach($generate_array as $m => $v){
    foreach($v as $p => $p_info){
        $where_array = array();
        if(!empty($p_info['term'])){
            foreach($p_info['term'] as $field => $value){
                $where_array[] = "`{$field}`='{$value}'";
            }
        }
        $select_sql = 'SELECT '.(empty($p_info['select']) ? '*' : '`'.implode('`,`',$p_info['select']).'`' )
                    .' FROM `'.$p_info['table'].'` '
                    .(empty($where_array) ? '' : 'WHERE '.implode(' AND ',$where_array) )
                    .(empty($p_info['order_by']) ? '' : ' ORDER BY `'.implode('`,`',$p_info['order_by']).'` '.$p_info['order_type'] );
        $query = $db->query($select_sql);
        
        for($i = 0, $row = $db->fetch_array($query); !empty($row) && $i < $max; $i++, $count++, $row = $db->fetch_array($query)){
            
            $parameter = array();
            foreach($p_info['parameter'] as $name => $value){
                $parameter[$name] = isset($row[$value]) ? $row[$value] : $value;
            }
            $row['url'] = php2html($m, $p, $parameter, $p_info['parameter_name'], true, 'Ym');
            if($row['url']){
                $result = '<font color="green">成功</font>';
                if(!empty($p_info['update'])){
                    $update_array = array();
                    foreach($p_info['update'] as $field => $value){
                        $update_array[] = '`'.$field.'`=\''.(isset($row[$value]) ? $row[$value] : $value).'\'';
                    }
                    $update_sql = 'UPDATE `'.$p_info['table'].'` SET '.implode(',',$update_array).' WHERE `id`=\''.$row['id'].'\'';
                    $result = $db->query($update_sql) ? '<font color="green">成功</font>' : '<font color="red">失败</font>';
                }
            }else{
                $result = '<font color="red">失败</font>';
            }
            echo "模块：{$m} 页面：{$p} ID：{$row[id]} 生成{$result}<br />SELECT SQL：{$select_sql}<br />UPDATE SQL：{$update_sql}<hr />";
            ob_flush();
            flush();
        }
    }
}
echo "共生成 {$count} 个页面";

function logs(){
    return;
}
?>
